/*
 *  $Id: FaerieSpark.cs 1051 2010-05-18 22:10:41Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Air.Spells {
    public class FaerieSpark: SpellCard<UnionData> {
        public FaerieSpark(): base("001_a_s_faerie_spark") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Air;
            Value = 100;
            Target = new UnionTarget(GetTargetPhrase(), PlayerTarget.Enemy, CreatureTarget.Enemy);
            AddManaCost(ElementType.Air, 3);
            AddCondition(new DelegateCondition(GetNewPhrase("not_faeries"), IsFaerieExists));
        }

        protected override void DoAction(TargetArgs<UnionData> args) {
            AbstractObject target = null;
            TargetArgs<Player> pa;
            TargetArgs<Creature> ca;
            switch (args.Target.GetArgs<Player, Creature>(out pa, out ca)) {
                case 1:
                    target = pa.Target;
                    break;
                case 2:
                    target = ca.Target;
                    break;
                default:
                    return;
            }
            int dmg = GetAllFaeriesCount(args.Player);
            if (dmg > 0) target.DamageBy(NewDamage(args, dmg));
        }

        private static bool IsFaerieExists(AbstractObject caster, Card card) {
            return GetAllFaeriesCount(caster.Owner) > 0;
        }

        private static int GetAllFaeriesCount(Player owner) {
            return GetFaeriesCount(owner) + GetFaeriesCount(owner.Enemy);
        }
        private static int GetFaeriesCount(Player owner) {
            int count = 0;
            foreach (Creature cr in owner.GetCreatures())
                if (cr.Race.IsRace<Races.Faerie>())
                    count++;
            return count;
        }
    }
}
